package org.supda.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.supda.system.dao.ResourceDao;
import org.supda.system.dao.RoleDao;
import org.supda.system.entity.TResources;
import org.supda.system.entity.TRole;
import org.supda.system.service.RoleService;
import org.supda.system.util.DataTablePageUtil;

@Service("RoleService")
@Transactional
public class RoleServiceImpl implements RoleService{

	@Autowired private RoleDao roleDao;
	@Autowired private ResourceDao resourceDao;
	
	@Override
	public TRole createRole(TRole role) {
		return roleDao.get(TRole.class, (Long)roleDao.save(role));
	}

	@Override
	public void deleteRole(Long roleId) {
		roleDao.delete(roleDao.get(TRole.class,roleId));
	}

	@Override
	public void correlationResources(Long roleId, List<Long> newResourceId) {
		
		TRole role = roleDao.get(TRole.class, roleId);
		
		for(Long resourceId : newResourceId){
			
			TResources  tResources = resourceDao.get(TResources.class, resourceId);
			
			role.getTResourceses().add(tResources);
			tResources.getTRoles().add(role);
		
		}
	}

	@Override
	public void unCorrelationResources(Long roleId, List<Long> unResourceId) {
		
		TRole role = roleDao.get(TRole.class, roleId);
		
		for(Long resourceId : unResourceId){
			
			TResources  tResources = resourceDao.get(TResources.class, resourceId);
			//解除权限和角色之间的关系
			role.getTResourceses().remove(tResources);
			tResources.getTRoles().remove(role);
		}
	}

	@Override
	public void listByDataTable(DataTablePageUtil<TRole> dtp) {
		roleDao.listByDataTable(dtp, TRole.class);
	}

	@Override
	public List<TResources> selectRoleCorrelationResources(Long roleId) {
		
		TRole role = findByRoleId(roleId);
		
		List<TResources> tResources = new ArrayList<TResources>();
		
		if(role!=null){
			for(TResources resource : role.getTResourceses()){
				if(resource!=null)
					tResources.add(resource);
			}
		}
		
		return tResources;
	}

	@Override
	public TRole findByRoleId(Long roleId) {
		List<TRole> roles =  roleDao.find("FROM TRole WHERE id=? ", new Object[]{roleId});
		return roles!=null?roles.size()>0?roles.get(0):null:null;
	}

	@Override
	public void updateRelationResources(Long roleId, List<Long> unResourceId,
			 List<Long> newResourceId) {
		unCorrelationResources(roleId, unResourceId);
		correlationResources(roleId, newResourceId);
	}

	@Override
	public List<TRole> findAllRole() {
		return roleDao.find("FROM TRole");
	}

	@Override
	public TRole createRole(TRole role, Long[] resIds) {

		role.setRoleKey(UUID.randomUUID().toString());
		role.setValid(Short.parseShort("1"));
		
		//保存
		TRole newRole = createRole(role);
		
		correlationResources(newRole.getId(), Arrays.asList(resIds));
		
		return newRole;
	}

	@Override
	public void deleteRoleLogic(Long id) {
		if(id == null && id == 0){
			throw new RuntimeException();
		}
		TRole role = findByRoleId(id);
		
		role.setValid(Short.parseShort("0"));
	}

	@Override
	public String locked(Long id) {
		
		TRole role = findByRoleId(id);
		
		if(role.getLocked() == 1){
			role.setLocked(Short.parseShort("0"));
			return "locked";
		}else{
			role.setLocked(Short.parseShort("1"));
			return "unlocked";
		}
	}
}
